<template>
	<view class="create-payment-bill-layout">
		<xh-input showTitle title="选择调拨订单" placeholder="请选择" minWidth="200rpx" isSelect @didSelect="showSelectOrder" v-model="totalCount" inputTextAlign="right"></xh-input>
		<view style="margin-top: 20rpx;">
			<view class="create-payment-bill-item">
				<text>货款金额</text>
				<text>¥{{totalAmount}}</text>
				<view class="line"></view>
			</view>
			<view class="create-payment-bill-item">
				<text>未结货款</text>
				<xh-input placeholder="请输入" inputTextAlign="right" :isLine="false" padding="0" height="40rpx" v-model="inputValue"></xh-input>
			</view>
		</view>
		 <view class="create-payment-bill-tip">
		 	<text>温馨提示</text>
			<text>1.货款金额=划拨总金额-回拨总金额；</text>
			<text>2.未结货款不超过货款金额；</text>
			<text>3.1条调拨订单仅能生成一次；</text>
		 </view>
		<view class="submit-btn" style="margin-top: 50rpx;" @tap="next">下一步</view>
	</view>
</template>

<script setup>
	import { onLoad } from '@dcloudio/uni-app'
	import { ref,computed } from 'vue'
	const props = defineProps(['agentId'])
	// 已选择的调拨订单
	let selectOrderList = ref([])
	const totalCount = ref('')
	// 输入的未结货款
	const inputValue = ref('')
	const totalAmount = computed(()=>{
		let totalMoney = 0.0
		selectOrderList.value.forEach(item=>{
			totalMoney += item.allotAmount
		})
		return totalMoney
	})
	// 选择调拨订单
	const showSelectOrder = ()=>{
		uni.navigateTo({
			url:`/pages/home/agent-manager/agent-info/create-payment-bill/select-allocated-order/select-allocated-order?agentId=${props.agentId}`
		})
	}
	// 下一步
	const next = ()=>{
		if(selectOrderList.value.length == 0){
			uni.$utils.showToast('请选择调拨订单')
			return
		}
		else if(uni.$uv.test.empty(inputValue.value)){
			uni.$utils.showToast('请输入未结货款')
			return
		}
		let params = {}
		params.agentId = props.agentId
		params.amount = totalAmount.value
		params.unAmount = uni.$uv.trim(inputValue.value,'all')
		params.list = selectOrderList.value
		const dataStr = encodeURIComponent(JSON.stringify(params))
		uni.navigateTo({
			url:`/pages/home/agent-manager/agent-info/create-payment-bill/create-payment-bill-next/create-payment-bill-next?data=${dataStr}`
		})
	}
	
	onLoad(()=>{
		uni.$off('selectOrderList')
		uni.$on('selectOrderList',(res)=>{
			console.log('已选择的调拨订单：',res)
			selectOrderList.value = res
			totalCount.value = selectOrderList.value.length > 0 ? `已选择${selectOrderList.value.length}条` : ''
		})
	})
</script>

<style lang="scss" scoped>
.create-payment-bill-layout{
	padding-top: 1px;
	.create-payment-bill-item {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 30rpx;
		background-color: #fff;
		position: relative;
	}
	.create-payment-bill-tip{
		display: flex;
		flex-direction: column;
		padding: 30rpx;
		color: $xh-color-9;
		font-size: 24rpx;
		text:nth-child(1){
			font-size: 30rpx;
			font-weight: bold;
			color: $xh-color-6;
			margin-bottom: 20rpx;
		}
		text:nth-child(n + 2){
			margin-top: 10rpx;
		}
	}
}
</style>
